import com.adobe.cairngorm.control.CairngormEventDispatcher;

import flash.events.*;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.URLRequest;

import mx.collections.ArrayCollection;
import mx.events.CloseEvent;
import mx.managers.PopUpManager;

import net.optix.ip.events.AddInventionEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.InventionVO;
            
            
[Bindable]
public var model:ModelLocator	=	ModelLocator.getInstance(); 
            
public var files:FileReferenceList	=	new FileReferenceList();
public var uploadRUL:URLRequest;
public var filesData:ArrayCollection	=	new ArrayCollection();
  
public function init():void
{
	model.bSubmit	=	true;
  	model.result	=	"";
 	model.close		=	"no";
}
  			
private function titleWindow_close(evt:CloseEvent):void 
{ 
	model.result	=	"";
  	model.bSubmit	=	true;
  	PopUpManager.removePopUp(this); 
}
            
           
            
private function selectHandler(event:Object):void
{
  	for(var i:int =0; i< files.fileList.length; i++)
	{
   		var f:FileReference	=	FileReference(files.fileList[i]);
            		
   		var obj:Object	=	new Object();
      	obj.fileName	=	f.name; 
     	obj.fileSize	=	f.size+"B";
      	obj.fileType	=	f.type;
      	obj.fileRefrence = f;
        obj.fileStatus	=	0;
        		
      	filesData.addItem(obj);
 	}
   	selectedFiles.validateNow();
}
            
private function chooseFile():void
{
  	var url:String	=	model.url+"/Invention/UploadAction/";
 	uploadRUL		=	new URLRequest(url);
  	files.addEventListener(Event.SELECT,selectHandler);
  	files.browse();
}
            
public function uploadHandler(event:MouseEvent):void
{
 	var file:FileReference;
  	for(var i:int = 0 ; i<filesData.length; i++)
   	{
     	file	=	FileReference(filesData[i].fileRefrence);
      	file.upload(uploadRUL);
      	filesData[i].fileStatus	=	1;
  	}
}
            
public function addInvention():void
{
	var result:Boolean	=	true;
            	
	if(inventionTitle.text == "")
  	{
    	resultTitle.text	=	"This field is required";
      	result	=	false;
	}
  	else
  	{
     	resultTitle.text	=	"";
	}
            	
  	if(inventionKeyWords.text	==	"")
   	{
       	resultKeyWords.text	=	"This field is required";
      	result	=	false;
 	}
 	else
  	{
   		resultKeyWords.text	=	"";
  	}
            	
 	if(invented.text	==	"")
 	{
 		resultInvented.text	=	"This field is required";
    	result	=	false;
 	}
   	else
 	{
     	resultInvented.text	=	"";
 	}
            	
  	
    if(result == true)
    {
    	model.bSubmit	=	false;
	 	var vo:InventionVO	=	new InventionVO();
	 	
	 	vo.clientID		=	model.client.clientID;
	 	vo.inventorID	=	model.inventor.inventorID;
	 	
		vo.title		=	inventionTitle.text;
		vo.keyWords		=	inventionKeyWords.text;
		vo.docket		=	docket.text;
		vo.type			=	1;
		vo.status		=	0;
		vo.invented		=	invented.text;
		vo.fileNames	=	fileNames;
		vo.description	=	description.text;
	    
	    var fileNames:String	=	"";
		if(filesData != null)
		{
			for(var i:int=0; i<filesData.length; i++)
		  	{
		   		if(filesData[i].fileStatus	==	1)
		    	{
		    		fileNames	=	fileNames+"-"+filesData[i].fileName;
		    	}
		  	}
		}
		    
	
		vo.fileNames	=	fileNames;
		         	
		var event:AddInventionEvent	=	new AddInventionEvent(vo);
	   	CairngormEventDispatcher.getInstance().dispatchEvent(event);
    }      	
}